﻿using System.Collections.Generic;
using System;

namespace HREngine.Bots
{
    public partial class Behavior经典T7猎 : Behavior
    {
        public override string BehaviorName() { return "经典T7猎"; }
        PenalityManager penman = PenalityManager.Instance;
        // 核心，场面值
        public override float getPlayfieldValue(Playfield p)
        {
            float retval = 0;
            retval += getGeneralVal(p);
            retval += p.owncarddraw * 5;
            // 危险血线
            int hpboarder = 7;
            // 不考虑法强了
            if (p.enemyHeroName == HeroEnum.mage) retval += 4 * p.enemyspellpower;
            // 抢脸血线
            int aggroboarder = 30;
            retval += getHpValue(p, hpboarder, aggroboarder);
            // 出牌序列数量
            int count = p.playactions.Count;
            int ownActCount = 0;
            // 排序问题！！！！
            for (int i = 0; i < count; i++)
            {
                Action a = p.playactions[i];
                ownActCount++;
                switch (a.actionType)
                {
                    // 英雄攻击
                    case actionEnum.attackWithHero:
                        continue;
                    case actionEnum.useHeroPower:
                    case actionEnum.playcard:
                        break;
                    default:
                        continue;
                }
                switch (a.card.card.chnName)
                {
                    // 排序优先
                    case "饥饿的秃鹫":
                    case "追踪术":
                    case "照明弹":
                        retval -= 2 * i;
                        break;
                    case "稳固射击":
                    case "动物伙伴":
                        retval -= i ;
                        break;
                    case "阿古斯防御者":
                        retval += i;
                        break;
                }
            }
            retval += getSecretPenality(p); // 奥秘的影响
            p.value = retval;
            return retval;
        }

        // 发现卡的价值
        public override int getDiscoverVal(CardDB.Card card, Playfield p)
        {
            switch (card.chnName)
            {
                case "狼骑兵":
                    if (p.enemyHero.Hp <= 3) return 100;
                    return 20;
                case "杀戮命令":
                    if (p.enemyHero.Hp <= 3) return 100;
                    if (p.enemyHero.Hp <= 5)
                        foreach (Minion m  in p.ownMinions)
                        {
                            if (m.handcard.card.race == 20) return 100;
                        }
                    return 20;
                case "鹰角弓":
                    if (p.enemyHero.Hp <= 3 && p.ownWeapon.Angr > 0 && p.ownHero.Ready) return 100;
                    return 20;
                case "火车王里诺艾":
                    if (p.enemyHero.Hp <= 6) return 100;
                    return 30;
                case "麻风侏儒":
                case "飞刀杂耍者":
                case "铁喙猫头鹰":
                case "森林狼":
                    return 10;
                case "奥术傀儡":
                    if (p.enemyHero.Hp <= 4) return 100;
                    return 40;
                case "动物伙伴":
                    if (p.enemyHero.Hp <= 4) return 50;
                    return 40;
                case "关门放狗":
                case "爆炸陷阱":
                    return p.enemyMinions.Count * 10;
                case "照明弹":
                case "误导":
                case "叫嚣的中士":
                case "猎人印记":
                    return 1;
            }
            return 0;
        }

        // 敌方随从价值 主要等于 
        public override int getEnemyMinionValue(Minion m, Playfield p)
        {
            int retval = 5;
            if(m.Angr > 0)
                retval += m.Hp * 2;
            if (!m.frozen && !(m.cantAttack && m.name != CardDB.cardName.argentwatchman))
            {
                retval += m.Angr * 2;
                if (m.windfury) retval += m.Angr * 2;
                if (m.Angr > 4) retval += 20;
                if (m.Angr > 7) retval += 20;
            }
            if (m.silenced) return retval;
            if (m.taunt) retval += 5;
            if (m.divineshild) retval += m.Angr;
            if (m.divineshild && m.taunt) retval += 5;
            if (m.stealth) retval += 1;

            if (m.poisonous)
            {
                retval += 4;
                if (p.ownMinions.Count < p.enemyMinions.Count) retval += 10;
            }
            switch (m.handcard.card.chnName)
            {
                case "铸甲师":
                    retval += 50;
                    break;
                case "末日预言者":
                    foreach(Minion mm in p.ownMinions)
                    {
                        retval += (mm.Hp + mm.Angr) * 2;
                    }
                    break;
            }

            
            // 吸血怪很吓人
            if (m.lifesteal) retval += m.Angr * 3;

            if (m.name == CardDB.cardName.nerubianegg && m.Angr <= 3 && !m.taunt) retval = 0;
            return retval;
        }

        public override int getMyMinionValue(Minion m, Playfield p)
        {
            int retval = 2;
            retval += m.Hp * 2;
            if (!m.cantAttack || !m.Ready || !m.frozen)
            {
                retval += m.Angr;
            }
            else
            {
                retval += m.Angr / 4;
            }
            // 鱼人种族价值
            //if (m.handcard.card.race == 14)
            //{
            //    retval += m.Hp + m.Angr + 2;
            //}
            // 风怒价值
            if ((!m.playedThisTurn || m.rush == 1 || m.charge == 1) && m.windfury) retval += m.Angr;
            // 圣盾价值
            if (m.divineshild ) retval += m.Angr / 2 + 1;
            // 潜行价值
            if (m.stealth) retval += m.Angr / 3 + 1;
            // 吸血
            if (m.lifesteal) retval += m.Angr / 3 + 1;
            // 圣盾嘲讽
            if (m.divineshild && m.taunt) retval += 4;
            // retval += m.synergy;
            // retval += getSpeicalCardVal(m.handcard.card);
            return retval;
        }

        public override int getSirFinleyPriority(List<Handmanager.Handcard> discoverCards)
        {
            
            return -1; //comment out or remove this to set manual priority
            int sirFinleyChoice = -1;
            int tmp = int.MinValue;
            for (int i = 0; i < discoverCards.Count; i++)
            {
                CardDB.cardName name = discoverCards[i].card.name;
                if (SirFinleyPriorityList.ContainsKey(name) && SirFinleyPriorityList[name] > tmp)
                {
                    tmp = SirFinleyPriorityList[name];
                    sirFinleyChoice = i;
                }
            }
            return sirFinleyChoice;
        }
        public override int getSirFinleyPriority(CardDB.Card card)
        {
            return SirFinleyPriorityList[card.name];
        }

        private Dictionary<CardDB.cardName, int> SirFinleyPriorityList = new Dictionary<CardDB.cardName, int>
        {
            //{HeroPowerName, Priority}, where 0-9 = manual priority
            { CardDB.cardName.lesserheal, 0 }, 
            { CardDB.cardName.shapeshift, 6 },
            { CardDB.cardName.fireblast, 7 },
            { CardDB.cardName.totemiccall, 1 },
            { CardDB.cardName.lifetap, 9 },
            { CardDB.cardName.daggermastery, 5 },
            { CardDB.cardName.reinforce, 4 },
            { CardDB.cardName.armorup, 2 },
            { CardDB.cardName.steadyshot, 8 }
        };

        public override int getComboPenality(CardDB.Card card, Minion target, Playfield p)
        {
            // 无法选中
            if (target != null && target.untouchable)
            {
                return 100000;
            }

            // 初始惩罚值
            int pen = 0;
            pen += penman.getSilencePenality(card.name, target, p);
            if (card.Secret)
            {
                pen -= 3;
                if(p.ownWeapon.Durability > 0 && p.ownWeapon.card.chnName == "鹰角弓")
                {
                    pen -= 5;
                }
            }

            switch (card.chnName)
            {
                case "狼骑兵":
                    pen -= 5;
                    break;
                case "杀戮命令":
                    if (target != null && !target.isHero && target.Hp < 3) pen += 20;
                    break;
                case "幸运币":
                    pen += 3;
                    break;
                case "麻风侏儒":
                    pen -= 5;
                    break;
                case "铁喙猫头鹰":
                    if(target != null && target.handcard.card.chnName == "末日预言者" && !target.silenced )
                    {
                        pen -= 100;
                    }
                    break;
                case "森林狼":
                    pen += 5;
                    foreach(Minion m in p.ownMinions)
                    {
                        if(m.handcard.card.race == 20)
                        {
                            pen -= 3;
                        }
                    }
                    break;
                case "动物伙伴":
                    pen -= 21;
                    break;
                case "爆炸陷阱":
                    int killed = 0;
                    foreach(Minion m in p.enemyMinions)
                    {
                        if (m.Hp <= 2 && !m.divineshild) killed ++;
                        else pen -= 1;
                    }
                    if (killed > 2) pen -= 200;
                    else pen -= killed * 3;
                    pen -= 2;
                    break;
                case "飞刀杂耍者":
                    if (p.ownMaxMana < 3 && p.enemyMinions.Count == 0) pen -= 20;
                    // 白送
                    else if (p.ownMinions.Count == 0 && p.enemyMinions.Count > 0 && p.mana == 2) pen += 10;
                    break;
                case "鹰角弓":
                    if (p.ownWeapon.Durability > 0) pen += 10;
                    else pen -= 5;
                    break;
                case "照明弹":
                    if (p.enemySecretCount > 0) pen -= 10;
                    break;
                case "误导":
                    if (p.enemyMinions.Count > 0) pen -= 3;
                    if (p.enemyMinions.Count > 0 && p.enemyMinions[0].Angr > 4) pen -= 6;
                    break;
                case "关门放狗":
                    pen += 11;
                    pen -= p.enemyMinions.Count * 3;
                    break;
                case "叫嚣的中士":
                    if (target == null || !target.Ready) pen += 1;
                    break;
                case "火车王里诺艾":
                    pen += 60;
                    if(p.mana >= 7)
                    {
                        foreach(Handmanager.Handcard hc in p.owncards)
                        {
                            if(hc.card.chnName == "关门放狗")
                            {
                                pen -= 60;
                                break;
                            }
                        }
                    }
                    break;
                case "奥术傀儡":
                    pen += 8;
                    break;
                case "猎人印记":
                    if (!target.taunt || target.Hp < 3) pen += 5;
                    if (target.Hp > 3) pen -= target.Hp * 2;
                    break;
                //-----------------------------英雄技能-----------------------------------
                case "稳固射击":
                    if(p.ownMaxMana < 5)
                    foreach(Handmanager.Handcard hc in p.owncards)
                    {
                        if(hc.card.type == CardDB.cardtype.MOB && hc.card.chnName == "淡水鳄")
                        {
                            pen += 100;
                            break;
                        }
                    }
                    break;
            }
            return pen;
        }

        public override int getHpValue(Playfield p, int hpboarder, int aggroboarder)
        {
            int retval = 0;
            // 血线安全
            if (p.ownHero.Hp + p.ownHero.armor > hpboarder)
            {
                retval += (2 + p.ownHero.Hp + p.ownHero.armor - hpboarder) / 2;
            }
            // 快死了
            else
            {
                if (p.nextTurnWin()) retval -= (hpboarder + 1 - p.ownHero.Hp - p.ownHero.armor);
                else retval -= 2 * (hpboarder + 1 - p.ownHero.Hp - p.ownHero.armor) * (hpboarder + 1 - p.ownHero.Hp - p.ownHero.armor);
            }
            if (p.enemyHero.Hp + p.enemyHero.armor < 15)
            {
                retval += (15 - p.enemyHero.Hp - p.enemyHero.armor) * 5;
            }

            // 对手血线安全
            if (p.enemyHero.Hp + p.enemyHero.armor > aggroboarder)
            {
                retval += (-p.enemyHero.Hp - p.enemyHero.armor) * 2;
            }
            // 开始打脸
            else if (p.enemyHero.Hp + p.enemyHero.armor <= aggroboarder && p.enemyHero.Hp + p.enemyHero.armor > aggroboarder / 3)
            {
                retval += 4 * (aggroboarder + 1 - p.enemyHero.Hp - p.enemyHero.armor);
            }
            else
            {
                retval += 6 * (aggroboarder + 1 - p.enemyHero.Hp - p.enemyHero.armor);
            }
            return retval;
        }

    }
}